package com.wy.utils;

import com.github.pagehelper.util.StringUtil;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class OrderController {

    @Test
    public void test(){
        List<String> list = IOUtils.redaTxtFileToList("D:\\2021\\CSM-1\\day7\\src\\main\\resources\\data.txt");
        //把文档List<String>转换成List<Orer>
        ArrayList<Order> orders = new ArrayList<>();
        Order order = new Order();
        for (int i = 0; i < list.size(); i++) {
            String line = list.get(i);
            if(StringUtil.isNotEmpty(line)){
                String[] split = line.split(":");
                String value ="";
                if(split.length==2){
                    value =split[1];//获取到单行，根据：进行切割，取后边一个
                }
                if(i%7==0){
                    if(StringUtil.isNotEmpty(value)){
                        order.setCode(value);
                    }
                } else if(i%7==1){
                    if(StringUtil.isEmpty(value)){
                        try {
                            throw new Exception("名称不能为空");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    order.setName(value);
                } else if(i%7==2){
                    ArrayList<Shop> shops = new ArrayList<>();
                    if(StringUtil.isNotEmpty(value)){
                        String[] split1 = value.split(";");
                        for (String s : split1) {
                            Shop shop = new Shop();
                            String[] split2 = s.split("-");
                            shop.setName(split2[0]);
                            String price = split2[1].substring(1);
                            if(NumberUtils.isNumber(price)){
                                shop.setPrice(price);
                            } else{
                                try {
                                    throw new Exception("价格不合法");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            shops.add(shop);
                        }
                    }
                    order.setShops(shops);
                } else if(i%7==3){
                    order.setCreate_time(value);
                } else if(i%7==4){
                    order.setEnd_time(value);
                } else if(i%7==5){
                    User user = new User();
                    user.setName(value);
                    order.setUser(user);
                    orders.add(order);//打当前对象添加到集合中，重新创建1个新的对象
                    order = new Order();
                }
            }
        }
        for (Order order1 : orders) {
            System.out.println(order1);
        }

        
    }
}
